Page({
  /**
   * 页面的初始数据
   */
  data: {
    user_account: '',
    user_gender: '',
    user_description:'',
    user_phone: '151',
    user_type:'',
    user_card_number:''
  },


  //表单提交
  formSubmit(e) {
    var app = getApp();
    console.log(e.detail.value);
    let {user_gender,user_description,user_phone } =e.detail.value
    this.setData({
      user_phone,
      user_gender,
      user_description
    });
    // 将数据直接更新到数据库；
    // wx.showToast({
    //   title: '点击了提交',
    // })
    wx.request({
      url: app.globalData.baseUrl +'/user/updateInfo', // 仅为示例，并非真实的接口地址
      method: 'post',
      data: {
        userAccount:  wx.getStorageSync('localUserAccount'),
        userGender: user_gender,
        userDescription:user_description,
        userPhone:user_phone,
        // userDescription:e.detail.value.user_type,
        // userDescription:e.detail.value.user_description,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        wx.showToast({
          title: 'res' + res,
        })
        if (res.data == 0) {
          wx.showToast({
            title: '信息更新成功！',
            icon: 'none',
            duration: 2000
          })
          // var localUserAccount = that.data.username; //记录登录用户
          // wx.setStorageSync('localUserAccount', localUserAccount);
          // wx.navigateTo({
          //   url: '../student/student'
          // })
        } else {
          wx.showToast({
            title: '信息更新失败！',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
    // wx.showToast({
    //   title: '请求完成',
    // })
  },

  onLoad: function (options) {
    // todo 页面初始化时加载用户性别
    var that = this
    var app = getApp();
    var localUserAccount = wx.getStorageSync('localUserAccount')
    that.data.user_account =localUserAccount
    that.data.userAccount = localUserAccount
    that.setData({
      userAccount: that.data.userAccount
    })
    // wx.showToast({
    //   title: '11s11',
    // })

      // 请求用户信息并显示
      wx.request({
        url: app.globalData.baseUrl +'/user/userInfo', 
        method: 'post',
        data: {
          userAccount: localUserAccount,
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          wx.showToast({
            title: '3333',
          })

          console.log(res.data)
          if (res.data != null) {
            wx.showToast({
              title: '获取用户信息成功！',
              icon: 'none',
              duration: 2000
            })
            // that.data.user_gender = res.data.userGender
            // that.data.user_phone = res.data.userPhone 
            // that.data.user_description = res.data.userDescription
            // that.data.user_card_number = res.data.userCardNumber
// wx.showToast({
//   title: that.data.user_description
// })
            that.setData({
              user_phone: res.data.userPhone ,
              user_gender: res.data.userGender,
              user_description:res.data.userDescription,
              user_card_number: res.data.userCardNumber
              //  user_phone ,
              // user_gender,
              // user_description,
              // user_card_number
            });
            console.log('setdata后：')
            console.log(that.data.user_phone)
          } else {
            wx.showToast({
              title: '获取用户信息失败',
              icon: 'none',
              duration: 2000
            })
          }
        },
        fail(msg){
          console.log(msg)
        }
      })

      wx.showToast({
        title: '2222',
      })
  },
  bind_card: function(){
    
    wx.navigateTo({
      url: '../cards/cards?userAccount='+this.data.user_account,
    })

  },
  get_bills: function(){
    wx.navigateTo({
      url: '../bills/bills?cardNumber='+this.data.user_card_number,
    })
  },
  updateUserInfo: function(){
    if(this.data.user_gender == '' ){
      wx.showToast({
        title: '请选择性别',
        icon: 'none',
        duration: 1500
      })
      return
    }
    if(this.data.user_phone == '' ){
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 1500
      })
      return
    }
    
    wx.request({
      url: app.globalData.baseUrl +'/user/login', // 仅为示例，并非真实的接口地址
      method: 'post',
      data: {
        account: that.data.username,
        password: that.data.password
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data == 0) {
          wx.showToast({
            title: '登录成功！',
            icon: 'none',
            duration: 2000
          })

          var localUserAccount = that.data.username; //记录登录用户
          wx.setStorageSync('localUserAccount', localUserAccount);
          wx.navigateTo({
            url: '../student/student'
          })
        } else {
          wx.showToast({
            title: '登录失败',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  }
})